<?php

    if ( ! isset( $content_width ) ) $content_width = 960;
    
    // init
    function set_up(){

        require_once(get_template_directory() . '/inc/widgets.php');
        require_once(get_template_directory() . '/inc/customizer.php');

        // Register custom widgets
        add_action( 'widgets_init', function(){
            register_widget( 'Foo_Widget' );
        });

        /* Register primary menu */
        register_nav_menus(array(
            'primary' => __('Primary Menu', 'orange'),
        ));

        /* Register social menu */
        register_nav_menus(array(
            'social' => __('social Menu', 'orange'),
        ));

        // Support featured image
        add_theme_support( 'post-thumbnails' );
        add_theme_support( "title-tag" );
        // Support link
        // add_filter( 'pre_option_link_manager_enabled', '__return_true' );

        add_theme_support( 'automatic-feed-links' );

        add_theme_support( 'custom-background' );

        add_theme_support( 'custom-logo',  array(
            'height'      => 100,
            'width'       => 478,
            'flex-height' => true,
            'flex-width'  => true,
            'header-text' => array( 'site-title', 'site-description' )
        ) );

        add_theme_support( "custom-header");
        
        add_editor_style();
    }
    set_up();

    // 初始化widgets
    add_action( 'widgets_init', 'widgets_init' );
    function widgets_init() {
        register_sidebar( array(
            'name'          => __( 'Orange', 'orange' ),
            'id'            => 'orange',
            'class'         => 'sidebar',
            'description'   => __( 'Widgets in this area will be shown on all posts and pages.', 'orange' ),
            'before_widget' => '<li id="%1$s" class="widget %2$s">',
            'after_widget'  => '</li>',
            'before_title'  => '<h2 class="widgettitle">',
            'after_title'   => '</h2>',
        ) );
        register_sidebar( array(
            'name'          => __( 'Banana', 'orange' ),
            'id'            => 'banana',
            'class'         => 'banana-sidebar',
            'description'   => __( 'This is sidebar for banana.', 'orange' ),
            'before_widget' => '<li id="%1$s" class="widget %2$s">',
            'after_widget'  => '</li>',
            'before_title'  => '<h2 class="bananatitle">',
            'after_title'   => '</h2>',
        ) );
    }

    // load scripts
    function orange_scripts(){
        
        if ( is_singular() ) wp_enqueue_script( "comment-reply" );

        // css
        wp_enqueue_style('normalize', get_template_directory_uri() . '/css/normalize.css');
        wp_enqueue_style('style', get_template_directory_uri() . '/style.css',['normalize'],'1.0');
        wp_enqueue_style('bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css');
        wp_enqueue_style('animate', get_template_directory_uri() . '/css/animate.css');
        wp_enqueue_style('font-awesome', get_template_directory_uri() . '/css/font-awesome.min.css');

        // scripts
        wp_enqueue_script('jquery');
        wp_enqueue_script('bootstrap', get_template_directory_uri() . '/scripts/bootstrap.min.js',[],'1.0',true);
        wp_enqueue_script('orange', get_template_directory_uri() . '/scripts/orange.js',['bootstrap'],'1.0', true);

    }
    add_action('wp_enqueue_scripts', 'orange_scripts');


    function new_excerpt_more( $more ) {
        return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">' . __('Read More', 'orange') . '</a>';
    }
    add_filter( 'excerpt_more', 'new_excerpt_more' );

    // show menu
    function orange_wp_nav_menu(){
        return wp_nav_menu( array(
            // 'menu'           => '1',
            'theme_location' => 'primary',
            'menu_class'     => 'primary-menu',
            'menu_id'        => 'orange-menu',
            'container'      => 'div',
            'container_class'=> 'menu',
            'fallback_cb'    => 'no_exists_theme_location',
            'container_id'   => 'menu',
            // 'before'         => '<span class="glyphicon glyphicon-adjust"></span>',
            // 'after'          => '<span class="caret"></span>',
            // 'link_before'    => 'T',
            // 'link_after'     => 'T',
            'echo'           => true,
            // 'depth'          => 1,
            // 'items_wrap'     => '<ul id="%1$s" class="%2$s">%3$s</ul>'
        ) );
    }

    function no_exists_theme_location(){
        echo "There is no this menu.";
    }

    function seo(){

        if (is_home()) {
            $description = '湖南文沥征信数据服务有限公司（简称文沥），于2014年在长沙注册正式成立。文沥定位于数据时代领先的金融科技公司，以大数据技术风控为手段，连接整合企业实时经营数据，促进传统金融行业的产品创新，为金融机构效率的提升提供全面的金融数字化服务，使一部分小微企业实现普恵金融成为可能。';
            $keywords = get_option('blogdescription').','.get_option('blogname');
        }else{
            $description = get_the_title();
            $keywords = get_the_tag_list();
            if (empty($keywords)) {
                $keywords = '湖南文沥征信数据服务有限公司';
            }else{
                $keywords = preg_replace('/<a href=".*?" rel="tag">/i', ',', $keywords);
                $keywords = preg_replace('/<\/a>/i', '', $keywords);
                $keywords = trim($keywords,',');
            }
        }
        
        echo sprintf("<meta name='description' content='%s'><meta name='keywords' content='%s'>",$description,$keywords);

    }

 ?>